#include "Player.h"
#include "Game.h"

void Player::update(float frameTime)
{
	const InputHandler* inputHandler = game->getInputHandler();
	Game* instance = (Game*)game;
	
	// Get
	std::list<Node*> nodes = m_baseNode->getConnectedNodes();

	if (inputHandler->isKeyDown(DIK_RIGHT) && inputHandler->isKeyDown(DIK_SPACE))
	{
		if (m_baseNode != m_currentNode)
		{
			std::list<Node*>::iterator iter = nodes.begin();
			for (; iter != nodes.end(); ++iter)
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				anims->playAnimation("Target");

				Message message(m_currentNode, "Hacking", 0);
				MessageHandler::sendMessage(message);
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_RIGHT))
	{
		std::list<Node*>::iterator iter = nodes.begin();
		for (; iter != nodes.end(); ++iter)
		{
			Node* temp = (*iter);
			D3DXVECTOR3 nodePos = temp->getPosition();

			if (getPosition().y == nodePos.y && getPosition().x < nodePos.x)
			{
				m_currentNode = (*iter);
				anims->playAnimation("Focus");
				setPosition(m_currentNode->getPosition());
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_LEFT) && inputHandler->isKeyDown(DIK_SPACE))
	{
		if (m_baseNode != m_currentNode)
		{
			std::list<Node*>::iterator iter = nodes.begin();
			for (; iter != nodes.end(); ++iter)
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				anims->playAnimation("Target");

				Message message(m_currentNode, "Hacking", 0);
				MessageHandler::sendMessage(message);
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_LEFT))
	{
		std::list<Node*>::iterator iter = nodes.begin();
		for (; iter != nodes.end(); ++iter)
		{
			Node* temp = (*iter);
			D3DXVECTOR3 nodePos = temp->getPosition();

			if (getPosition().y == nodePos.y && getPosition().x > nodePos.x)
			{
				m_currentNode = (*iter);
				anims->playAnimation("Focus");
				setPosition(m_currentNode->getPosition());
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_DOWN) && inputHandler->isKeyDown(DIK_SPACE))
	{
		if (m_baseNode != m_currentNode)
		{
			std::list<Node*>::iterator iter = nodes.begin();
			for (; iter != nodes.end(); ++iter)
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				anims->playAnimation("Target");

				Message message(m_currentNode, "Hacking", 0);
				MessageHandler::sendMessage(message);
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_DOWN))
	{
		std::list<Node*>::iterator iter = nodes.begin();
		for (; iter != nodes.end(); ++iter)
		{
			Node* temp = (*iter);
			D3DXVECTOR3 nodePos = temp->getPosition();
			
			if (getPosition().x == nodePos.x && getPosition().y < nodePos.y)
			{
				m_currentNode = (*iter);
				anims->playAnimation("Focus");
				setPosition(m_currentNode->getPosition());
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_UP) && inputHandler->isKeyDown(DIK_SPACE))
	{
		if (m_baseNode != m_currentNode)
		{
			std::list<Node*>::iterator iter = nodes.begin();
			for (; iter != nodes.end(); ++iter)
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				anims->playAnimation("Target");

				Message message(m_currentNode, "Hacking", 0);
				MessageHandler::sendMessage(message);
			}
		}
	}
	else if (inputHandler->isKeyDown(DIK_UP))
	{
		std::list<Node*>::iterator iter = nodes.begin();
		for (; iter != nodes.end(); ++iter)
		{
			Node* temp = (*iter);
			D3DXVECTOR3 nodePos = temp->getPosition();

			if (getPosition().x == nodePos.x && getPosition().y > nodePos.y)
			{
				m_currentNode = (*iter);
				anims->playAnimation("Focus");
				setPosition(m_currentNode->getPosition());
			}
		}
	}
	else
	{
		m_currentNode = m_baseNode;
		setPosition(m_currentNode->getPosition());
	}

	if (m_currentNode == m_baseNode)
		anims->playAnimation("Focus");

	/** Update animations and particle. Every frame. */
	anims->update(frameTime);
	particle.update(frameTime);

	position = particle.position;
}

void Player::draw(ID3DXSprite* sprite)
{
	//First we work out the centre of the frame
	D3DXVECTOR2 centre;
	centre.x = (float)(anims->getCurrentAnimation()->getFrameWidth() / 2);
	centre.y = (float)(anims->getCurrentAnimation()->getFrameHeight() / 2);

	/* Now we create a transformation matrix. If flipped, we scale X by -1
	 * we also use the position vector in our transaction */
	D3DXMATRIX mat;
	/*if(flip)
		D3DXMatrixTransformation2D(&mat, &centre, 0,
								   &D3DXVECTOR2(-1, 1), 0, 0,
								   &D3DXVECTOR2(position.x, position.y));
	else */
		D3DXMatrixTransformation2D(&mat, 0, 0, 0, 0, 0,
								   &D3DXVECTOR2(position.x, position.y));

	//Now we use this transformation to draw with, we set the draw position to 0 to avoid issues
	
	anims->draw(sprite, D3DXVECTOR3(0, 0, 0), mat);
}

void Player::handleMessage(Message message)
{
	// HANDLE MESSAGES...
	if (message.message == "NodeSecured")
	{
		Node* node = (Node*)message.data;
		
		for (int i = 0; i < m_hackedNodes.size(); ++i)
		{
			if (m_hackedNodes.front() == node)
			{
				m_hackedNodes.pop_front();
				i = m_hackedNodes.size();
			}
			else
				m_hackedNodes.pop_front();
		}
		// Implement knocking nodes off hacked list.
	}

	if (message.message == "NodeHacked")
	{
		Node* node = (Node*)message.data;
		m_hackedNodes.push_front(node);
		m_baseNode = node;
	}
}